<?php
namespace Orm\UserAddress;

use Base\Exception\RmException;
class UserAddressClass extends \Base\BaseOrmObject{
    protected   $id,
				$uid = 0, // 用户ID
				$province_id = 0,
				$city_id = 0,
				$area_id = 0,
				$province = '', // 省份
				$city = '', // 城市
				$area = '', // 区域/县
				$address = '', // 收货详细地址
				$ship_username = '', // 收货人名称
				$ship_mobile = '', // 收货人电话
				$is_del = 0,
				$insert_time = '',
				$update_time = '';

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\UserAddress\UserAddressModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'uid' => $this->uid,
			'province_id' => $this->province_id,
			'city_id' => $this->city_id,
			'area_id' => $this->area_id,
			'province' => $this->province,
			'city' => $this->city,
			'area' => $this->area,
			'address' => $this->address,
			'ship_username' => $this->ship_username,
			'ship_mobile' => $this->ship_mobile,
        );

        $model = new \Orm\UserAddress\UserAddressModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'uid' => $this->uid,
			'province_id' => $this->province_id,
			'city_id' => $this->city_id,
			'area_id' => $this->area_id,
			'province' => $this->province,
			'city' => $this->city,
			'area' => $this->area,
			'address' => $this->address,
			'ship_username' => $this->ship_username,
			'ship_mobile' => $this->ship_mobile,
        );

        $model = new \Orm\UserAddress\UserAddressModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\UserAddress\UserAddressModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}